
const int inf = 2e9;
struct Info {
	int mi, mx;
	Info(): mi(inf), mx(-inf) {}
  Info(int mi_, int mx_) : mi(mi_), mx(mx_) {} 
};
Info operator+(const Info &lhs, const Info &rhs) {
	Info t;
	t.mx = max(lhs.mx, rhs.mx);
  t.mi = min(lhs.mi, rhs.mi);
	return t;
}

void solve()
{
  int n;
  cin >> n;

  vi a(n), b(n);
  vector<Info> V(n);
  rep(i, 0, n - 1) {
    cin >> a[i];
  }
  rep(i, 0, n - 1) {
    cin >> b[i];
    V[i] = Info(b[i], a[i]);
  }

  ll ans = 0;
  RMQ<Info> rg(V);
  rep(i, 0, n - 1) {
    int l = i, r = n - 1, pl = n + 1;
    while (l <= r) {
      int mid = (l + r) >> 1;
      auto t = rg.query(i, mid);
      if (t.mx < t.mi) {
        l = mid + 1;
      } else {
        if (t.mx == t.mi) {
          pl = min(pl, mid);
        }
        r = mid - 1;
      }
    }

    if (pl != n + 1) {
      l = i, r = n - 1;
      int pr = pl;
      while (l <= r) {
        int mid = (l + r) >> 1;
        auto t = rg.query(i, mid);
        if (t.mx <= t.mi) {
          if (t.mx == t.mi) {
            pr = max(pr, mid);
          }
          l = mid + 1;
        } else {
          r = mid - 1;
        }
      }
      ans += pr - pl + 1;
    }
  }
  cout << ans << '\n';
}